package com.bit.book.controller;

import com.bit.book.constants.Constants;
import com.bit.book.model.UserInfo;
import com.bit.book.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    /**
     *
     * 用户名称
     * @param name
     * 用户密码
     * @param password
     * @param session
     * @return 如果校验成功返回true，否则返回false
     */
    @Autowired
    private UserService userService;


    @RequestMapping("/login")
    public Boolean login(String name, String password, HttpSession session){
        if(!StringUtils.hasLength(name) || !StringUtils.hasLength(password)){
            return false;
        }
        log.info("session_user_key"+name);
        UserInfo userInfo=userService.queryUserByName(name);
        log.info("userInfo:"+userInfo);
        if(userInfo==null){
            return false;
        }
        if(userInfo!=null&&userInfo.getUserName().equals(name)
                &&userInfo.getPassword().equals(password)){
            session.setAttribute(Constants.SESSION_USER_KEY,userInfo);
            return true;
        }
        return false;
        //校验账号密码是否正确
        //TODO 暂且硬编码
//        if ("admin".equals(name) && "admin".equals(password)){
//            session.setAttribute("name", name);
//            return true;
//        }
    }
}
